package com.loong.musicdemo.ui.account.MyCenter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.loong.musicdemo.R;
import com.loong.musicdemo.adapter.PageSongListAdapter;
import com.loong.musicdemo.model.LocalPlay;
import com.loong.musicdemo.model.Song;
import com.loong.musicdemo.util.deleteutil.DeleteMusicUtil;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import static org.litepal.LitePalApplication.getContext;

public class LocalMusicActivity extends AppCompatActivity {
    private List<Song> localList = new ArrayList<>();//本地歌曲列表
    private PageSongListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //背景图片融合
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN |
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        setContentView(R.layout.activity_local_music);
        Toolbar toolbar = findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("本地音乐");
            actionBar.setDisplayHomeAsUpEnabled(true);//若工具栏可用,则显示Home菜单
        }
        //获取数据库信息
        initLocal();
        RecyclerView recyclerLocalList = findViewById(R.id.current_list_recycler);
        adapter = new PageSongListAdapter(localList);
        //设置点击事件等
        adapter.setOnClickSongItem(new PageSongListAdapter.OnClickSongItem() {
            @Override
            public void onDeleteClick(int position) {
                //删除子项
                deleteDataById(localList.get(position).getSongId());
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onSongClick(int position) {
                //播放歌曲

            }
        });
        recyclerLocalList.setAdapter(adapter);
    }

    /**
     * 显示返回菜单栏
     *
     * @param item item
     * @return item
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;//返回前一个活动
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    /**
     * 删除id的数据库历史记录
     *
     * @param songId 歌曲id
     */
    private void deleteDataById(String songId) {
        boolean b = DeleteMusicUtil.deleteMusicById(songId);
        if (b) {
            Toast.makeText(getContext(), "删除成功", Toast.LENGTH_SHORT).show();
            localList.clear();//清空信息
            List<LocalPlay> localPlays = LitePal.findAll(LocalPlay.class);//本地音乐播放信息
            for (LocalPlay localPlay : localPlays) {
                Song song = new Song();
                song.setSongId(localPlay.getSongId());
                song.setSingerName(localPlay.getAuthorName());
                song.setName(localPlay.getSongName());
                localList.add(song);
            }
            adapter.notifyDataSetChanged();
        } else {
            Toast.makeText(getContext(), "删除失败", Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 从数据库中获取歌曲信息,
     * 再从内存中获取本地歌曲
     */
    private void initLocal() {
        List<LocalPlay> localPlays = LitePal.findAll(LocalPlay.class);//本地音乐播放信息
        for (LocalPlay localPlay : localPlays) {
            Song song = new Song();
            song.setSongId(localPlay.getSongId());
            song.setSingerName(localPlay.getAuthorName());
            song.setName(localPlay.getSongName());
            localList.add(song);
        }
    }
}